package fr.bordeaux1.planingdesetudiants.controler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fr.bordeaux1.planingdesetudiants.controler.db.IDBPlaningDesEtudiants;
import fr.bordeaux1.planingdesetudiants.model.Cours;
import fr.bordeaux1.planingdesetudiants.model.Etudiant;
import fr.bordeaux1.planingdesetudiants.model.Groupe;

public class EdtExporter {
	private static final String TABLE_HEAD = "<table border=\"1\" style=\"text-align: center;\"<tr><th>Horraire</th><th style=\"width:200px;\">8h-10h</th><th style=\"width:200px;\">10h-12h</th><td style=\"width:100px;\">pause</td><th style=\"width:200px;\">14h-16h</th><th style=\"width:200px;\">16h-18h</th></tr>";
	private static final String DOCTYPE = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><style type=\"text/css\">a{text-decoration:none;color:#0066CC;}</style> <title>";
	private IDBPlaningDesEtudiants bdd;
	private String repertoire;
	public EdtExporter(IDBPlaningDesEtudiants bdd,String repertoire) {
		this.bdd = bdd;
		this.repertoire = repertoire;
	}
	
	public void doExport() throws SQLException, IOException{
		long t1 = System.currentTimeMillis();
		List<Etudiant> etus = bdd.listerEtudiants();
		creerIndex(etus);
		
		for (Etudiant etudiant : etus) {
			creerFichierEtudiant(etudiant);
		}
		System.out.println("Export terminé en "+(System.currentTimeMillis()-t1)+"ms");
	}

	private void creerFichierEtudiant(Etudiant etudiant) throws IOException, SQLException {
		List<Groupe> groupes = bdd.listerGroupesEtudiant(etudiant);
		List<Cours> courss = new ArrayList<Cours>();
		
		for (Groupe grp : groupes) {
			courss.addAll(bdd.listerCoursDeGroupe(grp));
		}
		
		Writer writer = new OutputStreamWriter(new FileOutputStream(new File(repertoire+etudiant.getNumero()+".html")),"UTF8");
		writer.append(DOCTYPE);
		writer.append("Planning de "+formatCase(etudiant.getNom())+" "+ formatCase(etudiant.getPrenom()));
		writer.append("</title></head><body style=\"font-family: Verdana, sans-serif;\"><h3>Planning de "+formatCase(etudiant.getNom())+" "+ formatCase(etudiant.getPrenom())+"</h3>");
		writer.append(TABLE_HEAD);
		
		for (int jour = 0; jour < 5; jour++) {
			writer.append("<tr><th  style=\"height:100px;\">").append(CreneauFormater.getJour(jour)).append("</th>");

			for (int trancheHoraire = 0; trancheHoraire < 4; trancheHoraire++) {
				if(trancheHoraire==2){
					writer.append("<td></td>");
				}
				writer.append("<td  style=\"height:100px;\">");
				for (Cours cours : courss) {
					if(cours.getCreneau()==jour*4+trancheHoraire+1){
						writer.append(cours.getCodeUe());
						writer.append("<br/>");
						writer.append(bdd.chargerUe(cours.getCodeUe()).getLibelle());
						writer.append("<br/>");
						writer.append(cours.getType());
						writer.append("<br/>");
						writer.append(cours.getSalle());
					}
				}
				
				writer.append("</td>");
			}
			
			writer.append("</tr>");
		}
		
		writer.append("</table>");
		writer.flush();
		writer.close();
	}

	private void creerIndex(List<Etudiant> etus) throws IOException {
		
		Writer writer = new OutputStreamWriter(new FileOutputStream(new File(repertoire+"index.html")),"UTF8");
		writer.append(DOCTYPE);
		writer.append("Planning des étudiants");
		writer.append("</title></head><body style=\"font-family: Verdana, sans-serif;\"><h3>Liste des étudiants du planning</h3>");
		
		for (Etudiant etudiant : etus) {
			writer.append("<a href=\""+etudiant.getNumero()+".html\">"+formatCase(etudiant.getNom())+" "+ formatCase(etudiant.getPrenom())+"</a><br/>");
		}
		writer.flush();
		writer.close();
	}
	
	private String formatCase(String str){
		if(str.length()==0){
			return str;
		}
		return str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase();
	}
}
